#ifndef OMG_DDS_DATAREADER_HPP_
#define OMG_DDS_DATAREADER_HPP_

/* Copyright (c) 2009-2010, Real-Time Innovations, Inc.
 * Copyright (c) 2010, Object Management Group, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * - Redistributions of source code must retain the above copyright notice,
 *   this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 * - Neither the names of the above copyright holders nor the names of their
 *   contributors may be used to endorse or promote products derived from
 *   this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL A COPYRIGHT HOLDER OR CONTRIBUTOR BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */


template <typename DELEGATE>
class tdds::sub::DataReaderBase : public dds::core::Entity
{
    OMG_DDS_REFERENCE_TYPE(DataReaderBase)

public:
    void wait_for_historical_data(const dds::core::Duration &max_wait);

    dds::core::InstanceHandleSeq&
    get_matched_publications(dds::core::InstanceHandleSeq& publication_handles) const;

    dds::core::InstanceHandleSeq get_matched_publications() const;

    dds::domain::discovery::PublicationBuiltinTopicData
    get_matched_publication_data(
        const dds::core::InstanceHandle &publication_handle);

public:
    const dds::sub::SampleRejectedStatus get_sample_rejected_status();

    const dds::sub::LivelinessChangedStatus get_liveliness_changed_status();

    const dds::sub::RequestedDeadlineMissedStatus
    get_requested_deadline_missed_status();

    const dds::sub::RequestedIncompatibleQosStatus
    get_requested_incompatible_qos_status();

    const dds::sub::SubscriptionMatchedStatus
    get_subscription_matched_status();

    const dds::sub::SampleLostStatus get_sample_lost_status(); 

    // --- Entity methods: --- //
public:
    /**
     * This method closes the entity and releases all resources associated with
     * DDS, such as threads, sockets, buffers, etc. Any attempt to invoke
     * methods on a closed entity will raise an AlreadyClosedError.
     */
    virtual void close();

    using dds::core::Entity::set_qos;
    void set_qos(const dds::sub::DataReaderQos& qos);
    const dds::sub::DataReaderQos get_qos() const;

    dds::sub::Subscriber get_parent() const;

    // --- State: --- //
protected:
    friend class OMG_DDS_DATA_READER_DELEGATE;
    DELEGATE impl_;
};


template <typename T> 
class dds::sub::DataReader : public DataReaderBase
{
    OMG_DDS_REFERENCE_TYPE(DataReader)

public:
    typedef T                                       DataType;
    typedef Sample<DataType>                        SampleType;
    typedef LoanedSamples<DataType>                 LoanedSamplesType;
    typedef dds::core::StatusCondition<DataReader>  StatusConditionType;
    typedef ReadCondition<DataType>                 ReadConditionType;
    typedef QueryCondition<DataType>                QueryConditionType;

public:
    dds::topic::TopicDescription<DataType> get_topicdescription();
    
public:
    LoanedSamplesType read(
        int                             max_samples     = dds::core::LENGTH_UNLIMITED,
        const dds::core::SampleState&   sample_states   = dds::core::SampleState::any(), 
        const dds::core::ViewState&     view_states     = dds::core::ViewState::any(), 
        const dds::core::InstanceState& instance_states = dds::core::InstanceState::any());

    LoanedSamplesType take(
        int                             max_samples     = dds::core::LENGTH_UNLIMITED,
        const dds::core::SampleState&   sample_states   = dds::core::SampleState::any(), 
        const dds::core::ViewState&     view_states     = dds::core::ViewState::any(), 
        const dds::core::InstanceState& instance_states = dds::core::InstanceState::any());

    LoanedSamplesType read(
        const ReadConditionType& a_condition,
        int                      max_samples = dds::core::LENGTH_UNLIMITED);

    LoanedSamplesType take(
        const ReadConditionType& a_condition,
        int                      max_samples = dds::core::LENGTH_UNLIMITED);

    /**
     * Return true if data was read or false if none was available.
     */
    bool read_next(SampleType& sample);
    
    /**
     * Return true if data was taken or false if none was available.
     */
    bool take_next(SampleType& sample);

    LoanedSamplesType read_instance(
        const dds::core::InstanceHandle& a_handle,
        int                             max_samples     = dds::core::LENGTH_UNLIMITED,
        const dds::core::SampleState&   sample_states   = dds::core::SampleState::any(), 
        const dds::core::ViewState&     view_states     = dds::core::ViewState::any(), 
        const dds::core::InstanceState& instance_states = dds::core::InstanceState::any());

    LoanedSamplesType take_instance(
        const dds::core::InstanceHandle& a_handle,
        int                              max_samples     = dds::core::LENGTH_UNLIMITED,
        const dds::core::SampleState&    sample_states   = dds::core::SampleState::any(), 
        const dds::core::ViewState&      view_states     = dds::core::ViewState::any(), 
        const dds::core::InstanceState&  instance_states = dds::core::InstanceState::any());

    LoanedSamplesType read_next(
        const dds::core::InstanceHandle& previous_handle = dds::core::null,
        int                              max_samples     = dds::core::LENGTH_UNLIMITED,
        const dds::core::SampleState&    sample_states   = dds::core::SampleState::any(), 
        const dds::core::ViewState&      view_states     = dds::core::ViewState::any(), 
        const dds::core::InstanceState&  instance_states = dds::core::InstanceState::any());

    LoanedSamplesType take_next(
        const dds::core::InstanceHandle& previous_handle = dds::core::null,
        int                              max_samples     = dds::core::LENGTH_UNLIMITED,
        const dds::core::SampleState&    sample_states   = dds::core::SampleState::any(), 
        const dds::core::ViewState&      view_states     = dds::core::ViewState::any(), 
        const dds::core::InstanceState&  instance_states = dds::core::InstanceState::any());

    LoanedSamplesType read_next(
        const ReadConditionType&         a_condition,
        const dds::core::InstanceHandle& previous_handle = dds::core::null,
        int                              max_samples     = dds::core::LENGTH_UNLIMITED);

    LoanedSamplesType take_next(
        const ReadConditionType&         a_condition,
        const dds::core::InstanceHandle& previous_handle = dds::core::null,
        int                              max_samples     = dds::core::LENGTH_UNLIMITED);

public:
    ReadConditionType create_readcondition(
        const dds::core::SampleState&   sample_states   = dds::core::SampleState::any(), 
        const dds::core::ViewState&     view_states     = dds::core::ViewState::any(),
        const dds::core::InstanceState& instance_states = dds::core::InstanceState::any());
    
#ifdef OMG_DDS_CONTENT_SUBSCRIPTION_SUPPORT
    QueryConditionType create_querycondition(
        const std::string& query_expression,
        const dds::core::StringSeq& query_parameters,
        const dds::core::SampleState&   sample_states   = dds::core::SampleState::any(), 
        const dds::core::ViewState&     view_states     = dds::core::ViewState::any(),
        const dds::core::InstanceState& instance_states = dds::core::InstanceState::any());
#endif  // OMG_DDS_CONTENT_SUBSCRIPTION_SUPPORT

public:
    DataType& get_key_value(DataType& key_holder, 
                            const dds::core::InstanceHandle& handle) const;
    DataType get_key_value(const dds::core::InstanceHandle& handle) const;

    dds::core::InstanceHandle lookup_instance(const DataType& key_holder);

    // --- Entity methods: --- //
public:
    StatusConditionType get_statuscondition();

    void set_listener(DataReaderListener<DataType>* l,
                      const dds::core::StatusMask& mask = dds::core::StatusMask::all());

    DataReaderListener<DataType>* get_listener() const;
};


#endif // !defined(OMG_DDS_DATAREADER_HPP_)
